var appInstance = getApp();
Page({
  data: {
    kefudisplay:true,  
    wxcode:'',
    imgUrls: ['http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg','http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg','http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'],
    company:[{
      company_name:'公司名称',
      product_name:'产品名称',
      product_video:'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400'
    }],
    isSplot:true,
    navItems:[
      {
        name:'行业分类:',
        detail:'轻工业',
        url:'type_category'
      },
      {
        name:'行业细分:',
        detail:'石雕',
        url:'Industry_category',
      },
      {
        name:'品牌名:',
        detail:'品牌名',
        url:'brand_name'
      },
      {
        name:'规格:',
        detail:'规格',
        url:'type'
      },
      {
        name:'单位：',
        detail:'单位',
        url:'unit',
      },
      {
        name:'价格：',
        detail:'¥1999.00',
        url:'price'
      },
      {
        name:'产品描述：',
        detail:'该产品的描述',
        url:'detail',
      }
    ]
  },
  onLoad: function (e) {
    var that = this;
    console.log(e);
    appInstance.setPageUserInfo();  
    if(e.isSplot=='false'){that.setData({isSplot:true})}else{that.setData({isSplot:false})}  
    if(appInstance.globalData.firstlogin==false){
      that.productList(e.prodId)
    }
  },
  productList:function(e){
    return appInstance.productList(e);
  },
  company_nvg:function(){
    var isSplot = this.data.isSplot;
    // if(isSplot==false){return;};
    var param = appInstance.globalData.session_key;
    var userId = param.userId;
    var companyId = this.data.company[0].companyId;
    wx.navigateTo({
       url: '../companydetail/companydetail?userId='+userId+'&companyId='+companyId+'&isSplot='+isSplot,
       success: function(res){
         // success
       },
     })
  },
  change: function(e){
    var that = this;
    var isSplot = this.data.isSplot;
    if(!isSplot){console.log('not change');return false;}
    var op = e.currentTarget.dataset.op; 
     console.log(op);
     //获取所需要的数据格式
     var param={};
     param.imgUrls = this.data.imgUrls;
     param.navItems = this.data.navItems;
     param.company = this.data.company;
     appInstance.globalData.change_param = param;
     wx.navigateTo({
       url: '../addproduct/addproduct',
       success: function(res){
         // success
       },
     })
  },
    //获取二维码code图片地址
  // preview:function(){
  //   var that =this;
  //   var urls = [];
  //   urls[0] = that.data.wxcode;
  //   var param = that.data.wxcode;
  //   return appInstance.previewImage(param,urls);
  // },
  tofavorite:function(){
    var that = this;
    var company = this.data.company[0];
    var param = appInstance.globalData.session_key;
    var prodDetail = {
      source : '1',
      userId : param.userId,
      targetId : company.prodId,
      token  : param.myXcxSessionId,
      favoriteDesc:company.product_name,
      favoriteType:1
    }
    if(company.collectedSign==0){
      appInstance.sendRequest({
        url:'/cust/collectItem',
        data:prodDetail,
        method:'POST',
        success: function(res){
          // console.log(res);
          // console.log("qrCode")
          appInstance.showToast({
            title: '收藏成功',
            icon: 'success'
          })
          company.collectedSign=1;
           var company_arr = [];
          company_arr.push(company);
          that.setData({
            company:company_arr
          })
        }
      })
    }else{
      appInstance.sendRequest({
        url:'/cust/cancelCollectItem',
        data:prodDetail,
        method:'POST',
        success: function(res){
          console.log(res);
          appInstance.showToast({
            title: '取消收藏',
            icon: 'success'
          })
          company.collectedSign=0;
          var company_arr = [];
          company_arr.push(company);
          that.setData({
            company:company_arr
          })
        }
      })
    }
  },
  kefu:function(){
    var kefudisplay = this.data.kefudisplay;
     this.setData({
      kefudisplay:!kefudisplay
    })
  },
  content_call:function(e){
    console.log(e)
    return appInstance.makePhoneCall(e)
  },
  topdf:function(){
    var that = this;
    var company = this.data.company[0];
    if(company.mobileNo){
    wx.makePhoneCall({
      phoneNumber: company.mobileNo,
      success: function(res) {
        // success
      }
    })
    }else{
      appInstance.showModal({
            content: '供应商没有留号码，请使用其他联系方式'
          });
    }
    // var param = appInstance.globalData.session_key;
    // var prodDetail = {
    //   source : '1',
    //   userId : param.userId,
    //   productId : company.prodId,
    //   token  : param.myXcxSessionId,
    //   fileSource:1,
    // }
    // appInstance.sendRequest({
    //   url:'/prod/genProdAlbum',
    //   data:prodDetail,
    //   method:'POST',           
    //   header:{
    //     'content-type':'application/json'
    //   },
    //   success: function(res){
    //     console.log(res);
    //     wx.downloadFile({
    //       url: res.fileName, 
    //       success: function(res) {
    //         appInstance.showToast({
    //             title: '下载成功',
    //             icon: 'success'
    //         })
    //        },
    //        fail:function(res){
    //          console.log(res)
    //        }
    //    })     
    //   }
    // })
  },
  app_title: '石雕小程序',
  app_description: '石雕小程序',
  page_router: 'productdetail',
  onShareAppMessage: function(){
    var pageRouter = this.page_router;
    var prodId = this.data.company[0].prodId;
    return {
      title: this.app_title || '石雕小程序',
      desc: this.app_description || '',
      path: '/pages/'+pageRouter+'/'+pageRouter+'?isSplot=false&prodId='+prodId
    }
  }  
})